package com.sjm.bookshop.service.userloginservice;

import com.sjm.bookshop.dao.LoginDao;
import com.sjm.bookshop.entity.Admin;
import com.sjm.bookshop.entity.User;
import com.sjm.bookshop.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
@Transactional
public class LoginService {
    @Autowired
    private LoginDao loginDao;

    //    用户登录
    public User login(String phone, String password) {
        return loginDao.selectPhoneAndPassword(phone, password);
    }
//    注册

    //    管理员登录
    public Admin admin(String phone, String password) {
        return loginDao.adminLogin(phone, password);
    }

//    管理员注册
    public int addAdmin(Admin admin){
        String mdpwd=MD5Util.MD5(admin.getPassword());
        admin.setPassword(mdpwd);
        admin.setLogintime(new Date());
        return loginDao.addRegister(admin);
    }

    public int register(User user) {
        if((loginDao.checkUser(user.getPhone()) == null)){
            user.setPassword(MD5Util.MD5(user.getPassword()));
            user.setIsLocked(0);
            user.setCreatedtime(new Date());
            return loginDao.adduserRegister(user);
        }else {
            return 0;
        }

    }

    public int resetPwd(String phone, String password) {

      return   loginDao.resetPwd(phone,password);
    }
}